#pragma once
#include <fstream>
#include <map>
#include <iostream>
#include <filesystem>
#include <string>

// 命名空间别名，简化代码
namespace fs = std::filesystem;

struct SectionInfo {
    SectionInfo() = default;
    ~SectionInfo() {
        _section_datas.clear();
    }

    SectionInfo(const SectionInfo& src) {
        _section_datas = src._section_datas;
    }

    SectionInfo& operator = (const SectionInfo& src) {
        if (&src == this) {
            return *this;
        }

        this->_section_datas = src._section_datas;
        return *this;
    }

    std::map<std::string, std::string> _section_datas;

    std::string operator[](const std::string& key) {
        auto it = _section_datas.find(key);
        if (it == _section_datas.end()) {
            return "";
        }
        return it->second;
    }

    std::string GetValue(const std::string& key) {
        auto it = _section_datas.find(key);
        if (it == _section_datas.end()) {
            return "";
        }
        return it->second;
    }
};

class ConfigMgr
{
public:
    ~ConfigMgr() {
        _config_map.clear();
    }

    SectionInfo operator[](const std::string& section) {
        auto it = _config_map.find(section);
        if (it == _config_map.end()) {
            return SectionInfo();
        }
        return it->second;
    }

    ConfigMgr& operator=(const ConfigMgr& src) {
        if (&src == this) {
            return *this;
        }

        this->_config_map = src._config_map;
        return *this;
    }

    ConfigMgr(const ConfigMgr& src) {
        this->_config_map = src._config_map;
    }

    static ConfigMgr& Inst() {
        static ConfigMgr cfg_mgr;
        return cfg_mgr;
    }

    std::string GetValue(const std::string& section, const std::string& key);
    fs::path GetFileOutPath();
    void InitPath();

private:
    ConfigMgr();

    // 简单的INI文件解析函数
    bool ParseIniFile(const std::string& filename);

    // 存储section和key-value对的map
    std::map<std::string, SectionInfo> _config_map;

    // 路径变量，使用标准库的filesystem
    fs::path _log_path;
    fs::path _static_path;
    fs::path _bin_path;
};
